package com.groupscore.score.service;

import com.groupscore.score.dao.OptionDao;
import com.groupscore.score.dao.PersonalDao;
import com.groupscore.score.dao.PersonalScoreDao;
import com.groupscore.score.dao.ScoreDao;
import com.groupscore.score.pojo.Option;
import com.groupscore.score.pojo.Personal;
import com.groupscore.score.pojo.PersonalScore;
import com.groupscore.score.pojo.Score;
import com.groupscore.score.pojogroup.GroupScore;
import com.groupscore.score.pojogroup.OptionScore;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import util.IdWorker;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ScoreService {

    @Autowired
    private ScoreDao scoreDao;
    @Autowired
    private PersonalScoreDao personalScoreDao;
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TeamService teamService;

    @Autowired
    private LotterService lotterService;

    @Autowired
    private OptionDao optionDao;

    @Autowired
    private PersonalDao personalDao;

    /**
     * 小组抽签
     *
     * @return
     */
    @PostMapping(value = "/teamdraw")
    @CrossOrigin
    public Result teamdraw() {

        return new Result(true, StatusCode.OK, "小组抽签成功", teamService.findSearchTeam());
    }

    /**
     * 个人抽签
     *
     * @return
     */
    @GetMapping(value = "/persondraw")
    @CrossOrigin
    public Result persondraw() {
        return new Result(true, StatusCode.OK, "抽签成功！", lotterService.findSearchPerson());
    }

    /**
     * 打分
     */
    public void add(List<Score> scoreList, String userId) {

        for (Score s : scoreList) {
            Score score = new Score();
            score.setId(idWorker.nextId() + "");
            score.setGroupId(s.getGroupId());
            score.setOptionId(s.getOptionId());
            score.setUserId(userId);
            score.setScore(s.getScore());
            score.setContent(s.getContent());
            scoreDao.save(score);
        }
    }

    /**
     * 打分
     */
    public void addPersonalScore(List<OptionScore> optionScores, String userId) {

        for (OptionScore optionScore : optionScores) {

            PersonalScore personalScore = new PersonalScore();
            personalScore.setId(idWorker.nextId() + "");
            personalScore.setPersonalName(optionScore.getName());
            personalScore.setOptionId(optionScore.getOptionId());
            personalScore.setScore(optionScore.getScore());
            personalScore.setUserId(userId);
            personalScore.setContent(optionScore.getContent());
            personalScore.setCreateTime(new Date());

            personalScoreDao.save(personalScore);
        }
    }

    /**
     * 修改分数
     */
    public void update(Score score) {
        scoreDao.save(score);
    }

    /**
     * 删除分数
     */
    public void deleteById(String id) {
        scoreDao.deleteById(id);
    }

    /**
     * 查询小组得分,及排名
     */
    public List<Map> groupScore() {
        return scoreDao.groupScore();
    }


    public List<Map> groupScoreByUserId(String userId) {
        return scoreDao.groupScoreByUserId(userId);
    }

    public List<Map> allScore() {
        return scoreDao.allScore();
    }

    public List<Map> allPersonalScore() {
        return scoreDao.allPersonalScore();
    }


    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Map> findOption() {
        return optionDao.findOption();
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Option findById(String id) {
        return optionDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param
     */
    public void addOption(Option option) {
        option.setId(idWorker.nextId() + "");
        this.optionDao.save(option);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteOption(String id) {
        this.optionDao.deleteById(id);
    }

    /**
     * 修改
     *
     * @param option
     */
    public void updateOption(Option option) {
        this.optionDao.save(option);
    }

    /**
     * 循环插入比赛个人
     */
    @Transactional
    public void savePersonal(List<Personal> personalList) {

        //先删除原有名单
        personalDao.deleteAll();

        personalList.forEach(personal -> {
            personal.setId(idWorker.nextId() + "");
            personal.setCreateTime(new Date());
            personalDao.save(personal);
        });
    }

    public List<Map> personalList(){
        return personalDao.personalList();
    }
}
